package com.hucheng.activity.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.hucheng.activity.entity.UserPointDO;
import com.hucheng.activity.entity.UserPointDetailsDO;
import com.hucheng.activity.entity.UserPointExpiredDetailsDO;
import com.hucheng.activity.mapper.UserPointDetailsMapper;
import com.hucheng.activity.mapper.UserPointExpiredDetailsMapper;
import com.hucheng.activity.mapper.UserPointMapper;
import com.hucheng.activity.query.UserPointExpiredDetailsQuery;
import com.hucheng.activity.service.UserPointActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-07-11
 * @Description: 类描述
 **/
@Slf4j
@Component
@Service(interfaceClass = UserPointActivityService.class, retries = -1, timeout = 5000)
public class UserPointActivityServiceImpl implements UserPointActivityService {
    @Autowired
    private UserPointMapper userPointMapper;

    @Autowired
    private UserPointDetailsMapper userPointDetailsMapper;

    @Autowired
    private UserPointExpiredDetailsMapper userPointExpiredDetailsMapper;

    private ExecutorService service = Executors.newFixedThreadPool(10);

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subtractPoint(Integer point, String source, Integer userId) {
        log.info("扣除积分开始 >>>>>>>> 用户 " + userId + " 来源 " + source + " 积分 " + point);
        service.execute(() -> {
            //扣除积分
            UserPointDO userPointDO = new UserPointDO();
            userPointDO.setPoint(point);
            userPointDO.setUserId(userId);
            userPointMapper.updateSubtractPointByUserId(userPointDO);
            //新增积分明细
            UserPointDetailsDO userPointDetailsDO = new UserPointDetailsDO();
            userPointDetailsDO.setIsDel(0);
            userPointDetailsDO.setPoint(point);
            userPointDetailsDO.setExpiredTime(new Date());
            userPointDetailsDO.setPointType(2);
            userPointDetailsDO.setSource(source);
            userPointDetailsDO.setUserId(userId);
            userPointDetailsMapper.insert(userPointDetailsDO);
            //扣除过期积分明细
            int p = point;
            UserPointExpiredDetailsQuery userPointExpiredDetailsQuery = new UserPointExpiredDetailsQuery();
            userPointExpiredDetailsQuery.setUserId(userId);
            List<UserPointExpiredDetailsDO> lists = userPointExpiredDetailsMapper.list(userPointExpiredDetailsQuery);
            List<UserPointExpiredDetailsDO> updateLists = new ArrayList<>();
            for (UserPointExpiredDetailsDO userPointExpiredDetailsDO : lists) {
                if (userPointExpiredDetailsDO.getPoint() > p) {
                    userPointExpiredDetailsDO.setPoint(userPointExpiredDetailsDO.getPoint() - p);
                    updateLists.add(userPointExpiredDetailsDO);
                    break;
                } else {
                    p = p - userPointExpiredDetailsDO.getPoint();
                    userPointExpiredDetailsDO.setPoint(0);
                    updateLists.add(userPointExpiredDetailsDO);
                }
            }

            //批量更新过期积分
            userPointExpiredDetailsMapper.updateBatch(updateLists);
            log.info("扣除积分结束 >>>>>>>> 用户 " + userId + " 来源 " + source + " 积分 " + point);
        });

    }



}
